<?php
/**
 * Author: wutong
 */
class MsSQL {
	private static $instance;
	public $dbHost = ".";
	public $dbName = "wuxisafe";
	public $dbUser = "sa";
	public $dbPwd = "sa";
	public $dbPort = "";
	private $dbConn;
	private $result;
	private $sql;
	private $sql_error;
	public $connOpt;
	
	private function __construct() {
	}
	public function open($dbName=null){
		if($dbName!=null){
			$this->dbName=$dbName;
		}
		$this->connOpt=array(
			"UID"=>$this->dbUser,
			"PWD"=>$this->dbPwd,
			"Database"=>$this->dbName,
			"CharacterSet"=>"UTF-8"
		);
		if (($this->dbConn=sqlsrv_connect($this->dbHost,$this->connOpt)) == false) {
			$err=sqlsrv_errors();
			echo '<div style="border:1px dotted #ccc;background-color:#fbfbfb;padding:15px;line-height:180%;">';
			foreach($err as $key){
				echo  '<div style="font-size:12px;color:#666;">'.$key['message']."</div>";
			}
			echo '</div>';
			exit();
		}	
	}
	public function select($sql){
		if(($result=sqlsrv_query($this->dbConn,$sql,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET )))!=false){
			$this->result=$result;
			return $result;	
		}else{
			return false;
		}
	}
	public function insert($sql){
		if(($result=sqlsrv_query($this->dbConn,$sql))!=false){
			$this->result=$result;
                        $insert_id_arr=sqlsrv_fetch_array(sqlsrv_query($this->dbConn,"SELECT @@IDENTITY AS insert_id"),SQLSRV_FETCH_ASSOC);
			return  $insert_id_arr['insert_id'];
		}else{
			return false;
		}
	}
	public function update($sql){
		if(($result=sqlsrv_query($this->dbConn,$sql))!=false){
			$this->result=$result;
			return $this->getAffectedRows($result);	
		}else{
			return false;
		}
	}
	public function delete($sql){
		if(($result=sqlsrv_query($this->dbConn,$sql))!=false){
			$this->result=$result;
			return $this->getAffectedRows($result);	
		}else{
			return false;
		}
	}
	//获取数据
	public function getRs($result=null,$fetch_type=SQLSRV_FETCH_ASSOC,$row=SQLSRV_SCROLL_NEXT){
		$result=$result==null ? $this->result : $result;
		return sqlsrv_fetch_array($result,$fetch_type,SQLSRV_SCROLL_NEXT);	
	}
	//获取所有数据
	public function getArray($result=null,$fetch_type=SQLSRV_FETCH_ASSOC,$row=SQLSRV_SCROLL_NEXT){
		$result=$result==null ? $this->result : $result;
		$arr=array();
		while($rs=sqlsrv_fetch_array($result,$fetch_type)){
			array_push($arr,$rs);
		}
		return $arr;	
	}
	//获取影响的函数
	public function getAffectedRows($result=null){
		$result=$result==null ? $this->result : $result;
		return sqlsrv_rows_affected($result);
	}
	/**
	 * Description 获取实例
	 */
	public static function getInstance() {
		if (self::$instance == null) {
			self::$instance = new MsSQL ();
		}
		return self::$instance;
	}
    /**
     * Description 获取结果集中的行数
     * @param #res $result
     * @return int
     */
    public function getRows($result = null){
        $result = $result == null ? $this->result : $result;
        return sqlsrv_num_rows($result);
    }
	//获取字段长度
	public function getFieldsLength($result = null){
		$result = $result == null ? $this->result : $result;
		return sqlsrv_num_fields($result);	
	}
	public function close(){
		sqlsrv_free_stmt($this->result);
		sqlsrv_close($this->dbConn);	
	}
	//获取字段信息
	public function getField($result = null){
		$result = $result == null ? $this->result : $result;
		return sqlsrv_field_metadata($result);
	}
	public function __toString(){
		return "MSSQL数据库连接类";	
	}
	//转换GB2312到UTF8编码
	public function convertToU($data){
		if(is_array($data)){
			$keys=array_keys($data);
			for($i=0;$i<count($data);$i++){
				$data[$keys[$i]]=$this->convertToU($data[$keys[$i]]);
			}
		}else{
			$data=iconv("gb2312","utf-8",$data);
		}
		return $data;
	}
}
?>